package com.zd.bms.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zd.bms.bean.vo.resp.IndexStatisRespVo;
import com.zd.bms.service.IIndexService;
import com.zd.common.bean.model.TicketInfo;
import com.zd.common.bean.model.TicketOrder;
import com.zd.common.bean.vo.BarDataVo;
import com.zd.common.bean.vo.PieDataVo;
import com.zd.common.common.enums.PayStatusEnum;
import com.zd.common.common.enums.TicketChannelEnum;
import com.zd.common.common.utils.DateTimeUtil;
import com.zd.common.dao.TicketInfoDao;
import com.zd.common.dao.TicketOrderDao;
import com.zd.common.dao.TicketOrderDetailDao;
import com.zd.common.dao.common.FindRequestEntity;
import com.zd.common.dao.common.WhereEnum;

@Service
public class IndexServiceImpl implements IIndexService {
	@Autowired
	private TicketOrderDetailDao ticketOrderDetailDao;

	@Autowired
	private TicketOrderDao ticketOrderDao;

	@Autowired
	private TicketInfoDao ticketInfoDao;

	@Override
	public IndexStatisRespVo indexStatis(Integer scenicSpotId) throws Exception {
		IndexStatisRespVo respVo = new IndexStatisRespVo();
		Date today = DateTimeUtil.string2Date(DateTimeUtil.date2String(new Date(), "yyyy-MM-dd 00:00:00"));

		FindRequestEntity findReq = new FindRequestEntity();
		if(scenicSpotId !=null){
			findReq.addWhere("scenic_spot_id", WhereEnum.EQ, scenicSpotId);
		}
		findReq.addWhere("last_verify_time", WhereEnum.GE, today);
		int entryNums = ticketOrderDetailDao.count(findReq.getWhereList());
		respVo.setEntryNums(entryNums);

		findReq = new FindRequestEntity();
		if(scenicSpotId !=null){
			findReq.addWhere("scenic_spot_id", WhereEnum.EQ, scenicSpotId);
		}
		findReq.addWhere("channel", WhereEnum.EQ, TicketChannelEnum.Platform_user.getKey());
		findReq.addWhere("status", WhereEnum.EQ, PayStatusEnum.Payment_success.getKey());
		findReq.addWhere("pay_time", WhereEnum.GE, today);
		int onlineSellNums = ticketOrderDao.dailyOrderStatis(findReq.getWhereList());
		respVo.setOnlineSellNums(onlineSellNums);

		findReq = new FindRequestEntity();
		if(scenicSpotId !=null){
			findReq.addWhere("scenic_spot_id", WhereEnum.EQ, scenicSpotId);
		}
		findReq.addWhere("channel", WhereEnum.NE, TicketChannelEnum.Platform_user.getKey());
		findReq.addWhere("status", WhereEnum.EQ, PayStatusEnum.Payment_success.getKey());
		findReq.addWhere("pay_time", WhereEnum.GE, today);
		int outlineSellNums = ticketOrderDao.dailyOrderStatis(findReq.getWhereList());
		respVo.setOutlineSellNums(outlineSellNums);

		//近7日统计
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6);
		Date last7Day = DateTimeUtil.string2Date(DateTimeUtil.date2String(calendar.getTime(), "yyyy-MM-dd 00:00:00"));
		findReq = new FindRequestEntity();
		if(scenicSpotId !=null){
			findReq.addWhere("scenic_spot_id", WhereEnum.EQ, scenicSpotId);
		}
		findReq.addWhere("status", WhereEnum.EQ, PayStatusEnum.Payment_success.getKey());
		findReq.addWhere("pay_time", WhereEnum.GE, last7Day);
		List<TicketOrder> sellTickStatisLast7DayList = ticketOrderDao.sellTickStatisLast7Day(findReq.getWhereList());

		/*
		 * 拼柱状图数据
		 */
		//柱状图分析
		//找出这些天卖出的所有票种
		List<TicketOrder> barOrignDataArray = new ArrayList<>();
		barOrignDataArray.addAll(sellTickStatisLast7DayList); //copy一份数据 用于处理
		Set<Integer> ticketIdSet = new HashSet<>();
		if (sellTickStatisLast7DayList != null && sellTickStatisLast7DayList.size() > 0) {
			for (TicketOrder ticketOrder : sellTickStatisLast7DayList) {
				ticketIdSet.add(ticketOrder.getTicketId());
			}
		}
		List<Integer> ticketIdArray = new ArrayList<>();
		ticketIdArray.addAll(ticketIdSet);
		List<String> tickNameList = new ArrayList<>();
		for (int ticketId : ticketIdArray) {
			TicketInfo tickInfo = ticketInfoDao.get(ticketId);
			tickNameList.add(tickInfo.getName());
		}

		//列出需要展示的天数
		List<String> dayArray = new ArrayList<>();
		for (int i = 6; i >= 0; i--) {
			Calendar _calendar = Calendar.getInstance();
			_calendar.set(Calendar.DAY_OF_YEAR, _calendar.get(Calendar.DAY_OF_YEAR) - i);
			dayArray.add(DateTimeUtil.date2String(_calendar.getTime(), "yyyy-MM-dd"));
		}

		//封装柱状图数据
		BarDataVo barData = new BarDataVo();
		barData.setxArray(dayArray); //X坐标数据
		barData.setyType(tickNameList);//展示的数据类型
		Map<String, List<Integer>> dataMap = new HashMap<>();
		for (int i = 0; i < ticketIdArray.size(); i++) {
			Integer ticketId = ticketIdArray.get(i);
			String ticketName = tickNameList.get(i);
			List<Integer> dataArray = new ArrayList<>(); //具体数据
			for (String dayStr : dayArray) {
				Iterator<TicketOrder> it = barOrignDataArray.iterator();
				boolean havaData = false;
				while (it.hasNext()) {
					TicketOrder ticketOrder = it.next();
					if (dayStr.equals(DateTimeUtil.date2String(ticketOrder.getPayTime(), "yyyy-MM-dd"))
							&& ticketId.equals(ticketOrder.getTicketId())) {
						dataArray.add(ticketOrder.getTicketNums());
						it.remove();
						havaData = true;
						break;
					}
				}
				if (!havaData) {
					dataArray.add(0);
				}
			}
			dataMap.put(ticketName, dataArray);
		}
		barData.setDataMap(dataMap);
		respVo.setSellTickStatisLast7DayList(barData);

		/*
		 * 拼饼状图数据
		 */
		PieDataVo pieData = new PieDataVo();
		pieData.setTypeArray(tickNameList);
		//使用柱状图分析数据	
		List<Map<String, Integer>> dataArray = new ArrayList<>();
		for (String name : tickNameList) {
			Map<String, Integer> pieDataMap = new HashMap<>();
			List<Integer> sellArray = dataMap.get(name);
			int total = 0;
			for (Integer num : sellArray) {
				num = (num == null ? 0 : num);
				total += num;
			}
			pieDataMap.put(name, total);
			dataArray.add(pieDataMap);
		}
		pieData.setDataArray(dataArray);
		respVo.setSellTickStatisPieData(pieData);

		/*
		 * 近7日售票渠道分析 
		 */
		findReq = new FindRequestEntity();
		if(scenicSpotId !=null){
			findReq.addWhere("scenic_spot_id", WhereEnum.EQ, scenicSpotId);
		}
		findReq.addWhere("status", WhereEnum.EQ, PayStatusEnum.Payment_success.getKey());
		findReq.addWhere("pay_time", WhereEnum.GE, last7Day);
		List<TicketOrder> sellTickChannelStatisLast7Day = ticketOrderDao
				.sellTickChannelStatisLast7Day(findReq.getWhereList());
		/*
		 * 拼饼状图数据
		 */
		List<String> sellChannelArray = new ArrayList<>();
		List<Integer> sellChannelIdArray = new ArrayList<>();
		for (TicketChannelEnum channel : TicketChannelEnum.values()) {
			sellChannelArray.add(channel.getValue());
			sellChannelIdArray.add(channel.getKey());
		}

		PieDataVo channelPieData = new PieDataVo();
		channelPieData.setTypeArray(sellChannelArray);
		//使用柱状图分析数据	
		List<Map<String, Integer>> channelPieDataArray = new ArrayList<>();
		for (int i = 0; i < sellChannelArray.size(); i++) {
			Map<String, Integer> channelPieDataMap = new HashMap<>();
			String channelValue = sellChannelArray.get(i);
			int channelKey = sellChannelIdArray.get(i);
			boolean havaData = false;
			for (TicketOrder ticketOrder : sellTickChannelStatisLast7Day) {
				if (channelKey == ticketOrder.getChannel()) {
					channelPieDataMap.put(channelValue, ticketOrder.getTicketNums());
					havaData = true;
					break;
				}
			}
			if(!havaData) {
				channelPieDataMap.put(channelValue, 0);
			}
			channelPieDataArray.add(channelPieDataMap);
		}
		channelPieData.setDataArray(channelPieDataArray);
		respVo.setChannelPieData(channelPieData);
		return respVo;
	}
}
